<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>轨道控制器</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        html,
        body {
            width: 100%;
            height: 100%;
            overflow: hidden;
        }
    </style>
</head>

<body>
    <script type="module">
        import * as THREE from 'three'
        import { OrbitControls } from "three/examples/jsm/controls/OrbitControls"
        const scene = new THREE.Scene();
        const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
        const renderer = new THREE.WebGLRenderer({
            antialias: true //消除锯齿状毛边
        });
        renderer.setSize(window.innerWidth, window.innerHeight);
        document.body.appendChild(renderer.domElement);
        camera.position.set(2, 100, 10)
        camera.lookAt(0, 0, 0)

        //网格线
        const gridHelper = new THREE.GridHelper(100, 10, 0xffffff)
        //添加一个立方体
        const cube = new THREE.Mesh(
            new THREE.BoxGeometry(10, 10, 10),
            new THREE.MeshBasicMaterial({ color: 0x00ff00 })
        );
        scene.add(gridHelper, cube)

        const controls = new OrbitControls(camera, renderer.domElement)
        controls.enableDamping = true //阻尼感 运动惯性
        controls.addEventListener('change', () => {
            renderer.render(scene, camera)
        })
        renderer.render(scene, camera)

        function animate() {
            //update配合enableDamping使用，才能应用这种惯性
            controls.update()
            // renderer.render(scene, camera)
            requestAnimationFrame(animate)
        }
        animate()
    </script>
</body>

</html>